<template>
  <div style="margin-top: 20px;">
    <el-card class="box-card">
      <!-- header -->
      <div slot="header" class="clearfix">
        <!-- v-model="activeName" @tab-click="handleClick" -->
        <!-- 左侧内容 -->
        <el-tabs class="tabs" v-model="activeName" @tab-click="tabClick">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visit"></el-tab-pane>
        </el-tabs>
        <!-- 右侧内容 -->
        <div class="date">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <el-date-picker
            v-model="date"
            class="date-picker"
            type="datetimerange"
            range-separator="|"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="mini"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </div>
      </div>
      <!-- content -->
      <div class="content">
        <el-row :gutter="10">
          <el-col :span="20">
            <!-- chart容器 -->
            <div class="chart" ref="chart"></div>
          </el-col>
          <el-col :span="4" class="rank">
            <h3>门店{{title}}排名</h3>
            <ul>
              <li>
                <span class="rindex">1</span>
                <span class="rname">店名1</span>
                <span class="rvalue">12234</span>
              </li>
              <li>
                <span class="rindex">2</span>
                <span class="rname">店名2</span>
                <span class="rvalue">12234</span>
              </li>
              <li>
                <span class="rindex">3</span>
                <span class="rname">店名3</span>
                <span class="rvalue">12234</span>
              </li>
              <li>
                <span>4</span>
                <span class="rname">店名4</span>
                <span class="rvalue">12234</span>
              </li>
              <li>
                <span>5</span>
                <span class="rname">店名5</span>
                <span class="rvalue">12234</span>
              </li>
              <li>
                <span>6</span>
                <span class="rname">店名6</span>
                <span class="rvalue">12234</span>
              </li>
              <li>
                <span>7</span>
                <span class="rname">店名7</span>
                <span class="rvalue">12234</span>
              </li>
            </ul>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
import echarts from 'echarts'
import dayjs from 'dayjs'

export default {
  name: 'Sale',
  data() {
    return {
      activeName: 'sale',
      chart: null,
      date: []
    }
  },
  mounted() {
    this.chart = echarts.init(this.$refs.chart)
    this.chart.setOption({
      title: {
        text: '销售额趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: ['一月 ', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          // name: 'Direct',
          type: 'bar',
          barWidth: '60%',
          data: [10, 52, 200, 334, 390, 330, 220, 45, 100, 340, 45, 65],
          color: 'skyblue'
        }
      ]
    })
  },
  computed: {
    title() {
      return this.activeName == 'sale' ? '销售额' : '访问量'
    }
  },
  methods: {
    tabClick() {
      if (this.activeName == 'sale') {
        this.chart.setOption({
          title: {
            text: '销售额趋势'
          },
          series: [
            {
              // name: 'Direct',
              type: 'bar',
              barWidth: '60%',
              data: [10, 52, 200, 334, 390, 330, 220, 45, 100, 340, 45, 65],
              color: 'skyblue'
            }
          ]
        })
      } else if (this.activeName == 'visit') {
        this.chart.setOption({
          title: {
            text: '访问量趋势'
          },
          series: [
            {
              // name: 'Direct',
              type: 'bar',
              barWidth: '60%',
              data: [34, 12, 20, 34, 312, 300, 400, 34, 200, 78, 43, 12],
              color: 'red'
            }
          ]
        })
      }
    },
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    }
  }
}
</script>

<style scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tabs {
  width: 100%;
}
.date {
  position: absolute;
  right: 0;
}
.date span {
  margin: 0 10px;
  font-size: 14px;
  cursor: pointer;
}
.date-picker {
  width: 300px;
  margin: 0 20px;
}
.chart {
  width: 100%;
  height: 300px;
}
.rank {
  padding: 0;
}
.rank h3 {
  margin: 0;
  font-size: 15px;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0;
}
ul li {
  height: 14%;
}
.rindex {
  display: inline-block;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: black;
  color: #fff;
  text-align: center;
  line-height: 20px;
}
.rname {
  margin-left: 20px;
}
.rvalue {
  float: right;
}
</style>